package com.car.slave.activity;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.car.slave.im.IMClient;
import com.car.slave.im.model.user.IMTextMessage;
import com.car.slave.im.model.user.IMUserMessage;
import com.car.slave.im.receiver.IMMessageReceiver.ExceptionStatus;
import com.car.slave.im.receiver.IMMessageReceiver.OnMessageCallback;
import com.car.slave.im.util.IMRecentMessage.IMRecentMessageCallback;
import com.car.slave.im.util.IMRecentMessage.IMRecentMessageResult;
import com.imchezhil.R;

public class ChatActivity extends Activity implements OnMessageCallback {
	
	private ListView mListView;
	
	private EditText content;
	private Button send;
	
	private ChatAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chat);
		
		//TODO 设置接受消息的回调
		IMClient.shareInstance().messageCallback(this, this);
		
		initView();
		initViewData();
		initViewListener();
	}
	
	private void initView() {
		mListView = (ListView) findViewById(R.id.listview);
		content = (EditText) findViewById(R.id.content);
		send = (Button) findViewById(R.id.send);
	}
	
	private void initViewData() {
		// 获取最近聊天记录
		/**
		List<IMUserMessage> msgs = IMClient.shareInstance().getRecentLocalMessages(this, 20);
		mAdapter = new ChatAdapter(this, msgs);
		mListView.setAdapter(mAdapter);
		*/
		
		IMClient.shareInstance().getRecentMessages(this, null, 20, new IMRecentMessageCallback() {
			@Override
			public void begin() {
				
			}

			@Override
			public void failed(IMRecentMessageResult result) {
				
			}

			@Override
			public void recentMessages(List<IMUserMessage> messages) {
				mAdapter = new ChatAdapter(ChatActivity.this, messages);
				mListView.setAdapter(mAdapter);
			}
		});
	}
	
	private void initViewListener() {
		send.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				IMTextMessage msg = IMClient.shareInstance().sendTextMessage(ChatActivity.this, content.getText().toString());
				content.setText("");
				mAdapter.addMessage(msg);
				mAdapter.notifyDataSetChanged();
				mListView.setSelection(mAdapter.getCount());
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//TODO 取消接受消息的回调
		IMClient.shareInstance().cancelMessageCallback(this);
	}

	// 接受到信息
	@Override
	public void receiveMessage(IMUserMessage message) {
		if (message.type == IMUserMessage.TYPE_TEXT) {
			IMTextMessage msg = (IMTextMessage) message;
			mAdapter.addMessage(msg);
			mAdapter.notifyDataSetChanged();
			mListView.setSelection(mAdapter.getCount());
		}
	}

	// 发送消息结果回调
	@Override
	public void sendMessageResult(IMUserMessage message, ExceptionStatus status) {
		// 去重复的Message
		List<IMUserMessage> messages = mAdapter.getData();
		int index = messages.indexOf(message);
		if (index != -1) {
			messages.set(index, message);
		} else {
			messages.add(message);
		}
		mAdapter.notifyDataSetChanged();
		mListView.setSelection(mAdapter.getCount());
	}
}
